package com.akaovi.courseExp.main;

import com.akaovi.courseExp.pojo.Student;
import com.akaovi.courseExp.utils.Utils;

import java.io.*;
import java.util.List;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/10/19 10:51
 * @since 2022/10/19 10:51
 */
public class Main {

    public static void main(String[] args) {

        List<Student> students = Utils.createStudentList();

        File file = new File("D:\\javaProject\\javaExp\\courseExp1\\src\\students.txt");

        // 写入文件
        ObjectOutputStream oos = null;
        // 读入文件
        ObjectInputStream ois = null;

        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));

            // 写出到文件
            oos.writeObject(students);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 读入学生信息
        try {
            ois = new ObjectInputStream(new FileInputStream("D:\\javaProject\\javaExp\\courseExp1\\src\\students.txt"));

            List<Student> students1 = (List<Student>) ois.readObject();

            double avr = 0.0;

            for (Student student : students1) {
                System.out.println(student);
                avr += student.getScore();
            }

            avr /= students1.size();

            System.out.printf("学生平均成绩: %f\n", avr);

        } catch (Exception e) {
            e.printStackTrace();
            return;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }
}
